<?php

namespace app\controller;

use app\BaseController;
use app\jwt\JWTS;
use app\model\HotelData;
use app\model\Img;
use app\model\Map;
use app\model\Message;
use app\model\Room;
use app\model\RoomDetail;
use app\Request;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Index extends BaseController
{
    //微信授权
    public function wxLogin(Request $request)
    {
//        获取code
        $code = $request->get('code');
//        获取昵称
        $nickName = "aa";
//        获取appId
        $appId = "wxa2469ff6402360ff";
//        获取appSecret
        $appSecret = "47732169fb165797f0303f1f781de142";
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        $output = json_decode($output, true);
        $session_key = $output['session_key'];
        $openid = $output['openid'];
//        查询是否存在openid
        $postdata = \app\model\Index::selectData($openid);
        $data = [
            'nickname' => $nickName,
            "openid" => $openid,
            "session_key" => $session_key
        ];
        if (!$postdata) {
            \app\model\Index::addData($data);
        } else {
            \app\model\Index::changeData($openid, $data);
        }
////        $key = "example_key";
//        $payload = array(
//            'id' => $postdata->id
//        );
//        $token = JWT::encode($payload, $key, 'HS256');
        $id = $postdata->id;
//        dd($id);
//        颁发token
        $token = \app\jwt\Jwt::createJwt($id);
        return json([
            'openid' => $data['openid'],
            'token' => $token,
            'id' => $id
        ]);
    }

    //列表
    public function listDatas(Request $request)
    {
        $id = $request->user_id;
//        dd($id);
        $pages = $request->get('pages');
        $data = HotelData::listDatas($pages);
        return json(['data' => $data]);
    }

    //轮播图
    public function img()
    {
        $img = Img::img();
        return json(['data' => $img]);
    }


//    public function add()
//    {
//        $uid = 1;
//        $token = JWTS::us($uid);
//        return json($token);
//    }
    //详情
    public function detail(Request $request)
    {
        $id = $request->get("id");
        $data = HotelData::detail($id);
        return json(['data' => $data]);
    }

    //地图
    public function map($list_id)
    {
        $data = Map::map($list_id);
        return json(['data' => $data]);
    }

    //房间列表
    public function room($list_id)
    {
        $data = Room::room($list_id);
        return json(['data' => $data]);
    }

    //房间详情
    public function roomDetail($id)
    {
        $data = Room::roomDetail($id);
        return json(['data' => $data]);
    }

    //添加入住人信息
    public function messageAdd(Request $request)
    {
        $postData = $request->param();
//        dd($postData['name']);
        if($postData['name']||$postData['tel']!=""){
            $data = Message::messageAdd($postData);
            return json(['data' => $data]);
        }
    }
    //查询入住人信息
    public function messageSelect($id)
    {
        $data = Message::messageSelect($id);
        return json(['data' => $data]);
    }
}
